package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.query.QueryEmpBo;
import cn.cqie.frame.entity.Emp;
import cn.cqie.frame.entity.SysUser;
import cn.cqie.frame.mapper.EmpMapper;
import cn.cqie.frame.service.EmpService;
import cn.cqie.frame.service.SysUserService;
import cn.cqie.frame.utils.AssertUtil;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.EmpVo;
import cn.cqie.frame.vo.UserVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-09
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpService empService;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增or更新员工信息
     * @param bo
     * @return
     */
    @Override
    public EmpVo saveOrUpdateEmp(Emp bo) {
        LocalDateTime sysTime = LocalDateTime.now();
        bo.setCreateTime(sysTime);
        bo.setUpdateTime(sysTime);
        boolean insert = empService.saveOrUpdate(bo);
        AssertUtil.isTrue(insert, "插入/更新菜单失败");
        return BeanUtil.toBean(bo, EmpVo.class);
    }

    /**
     * 分页查询员工信息
     * @param bo
     * @return
     */
    @Override
    public List<EmpVo> list(QueryEmpBo bo) {
        MyPageHelper.startPage(bo);
        List<EmpVo> empVos = empMapper.listEmp(bo);
        return entity2Vo(empVos);
    }

    /**
     * 删除员工信息
     * @param empId
     * @return
     */
    @Override
    public Boolean deleteEmp(Integer empId) {
        boolean b = empService.removeById(empId);
        return b;
    }

    /**
     * 查询员工对应的系统账号
     * @param empId
     * @return
     */
    @Override
    public List<UserVo> listUsers(Integer empId) {
        QueryWrapper<SysUser> lqw = new QueryWrapper<>();
        String notInSql = "select userId from emp where userId is not null";
        if (empId != null){
            notInSql+=" and id<>" + empId;
        }
        lqw.notIn("id",notInSql);
        List<SysUser> list = sysUserService.list(lqw);
        return Optional.ofNullable(list).orElseGet(() -> new ArrayList<>()).stream().map(it -> {
            UserVo userVo = new UserVo();
            userVo.setId(it.getId());
            userVo.setAccount(it.getAccount());
            return userVo;
        }).collect(Collectors.toList());
    }

    /**
     * 格式转换
     * @param collection
     * @return
     */
    private List<EmpVo> entity2Vo(Collection<EmpVo> collection) {
        List<EmpVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, EmpVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<EmpVo> page = (Page<EmpVo>) collection;
            Page<EmpVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }
}
